#include "xmlWidget.h"
#include "xmlWidgetLuaEngine.h"
using namespace xmlWidgetLib;

#include <iostream>
#include <string>
using namespace std;

#include "logManager.h"

/*
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*/
int L_widgetOpen(lua_State* L)
{
	const char *name = luaL_checkstring(L,-1);
	if ( NULL == name )
	{
		return 1;
	}
	logger << " call by lua script open widget <" << name << ">" << endl;

	xmlWidgetLuaEngine *luaEngine = xmlWidgetLuaEngine::getGlobalInstance();
	string key(name);
	xmlWidget * pWidget = luaEngine->searchWidget(key);
	if ( NULL == pWidget )
	{
		logger << " call by lua script can not get widget <" << name << ">" << endl;
		return 1;
	}
	pWidget->setHidden(false);
	pWidget->show();

	lua_pushnumber(L,0);
	return 1;
}
/*
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*/
int L_widgetClose(lua_State* L)
{
	const char *name = luaL_checkstring(L,-1);
	if ( NULL == name )
	{
		return 1;
	}
	logger << " call by lua script open widget <" << name << ">" << endl;

	xmlWidgetLuaEngine *luaEngine = xmlWidgetLuaEngine::getGlobalInstance();
	string key(name);
	xmlWidget * pWidget = luaEngine->searchWidget(key);
	if ( NULL == pWidget )
	{
		logger << " call by lua script can not get widget <" << name << ">" << endl;
		return 1;
	}
	pWidget->hide();

	lua_pushnumber(L,0);
	return 1;
}
/*
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*/
int L_fileSelectorGetName(lua_State* L)
{
	const char *name = luaL_checkstring(L,-1);
	if ( NULL == name )
	{
		return 1;
	}
	logger << " call by lua script open widget <" << name << ">" << endl;

	xmlWidgetLuaEngine *luaEngine = xmlWidgetLuaEngine::getGlobalInstance();
	string key(name);
	xmlWidget * pWidget = luaEngine->searchWidget(key);
	if ( NULL == pWidget )
	{
		logger << " call by lua script can not get widget <" << name << ">" << endl;
		return 1;
	}
	const char *fileName = gtk_file_selection_get_filename( GTK_FILE_SELECTION( pWidget->getGtkWidget()) );

	lua_pushstring(L,fileName);
	return 1;
}
/*
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*/
int L_fileSelectorSetName(lua_State* L)
{
	const char *name = luaL_checkstring(L,1);
	const char *fileName = luaL_checkstring(L,2);
	if ( NULL == name || NULL == fileName)
	{
		return 1;
	}
	logger << " call by lua script open widget <" << name << ">" << endl;

	xmlWidgetLuaEngine *luaEngine = xmlWidgetLuaEngine::getGlobalInstance();
	string key(name);
	xmlWidget * pWidget = luaEngine->searchWidget(key);
	if ( NULL == pWidget )
	{
		logger << " call by lua script can not get widget <" << name << ">" << endl;
		return 1;
	}
	
	gtk_file_selection_set_filename( GTK_FILE_SELECTION( pWidget->getGtkWidget()) ,fileName );

	lua_pushnumber(L,0);
	return 1;
}
/*
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*******************************************************************************************
*/

xmlWidgetLuaEngine *xmlWidgetLuaEngine::getGlobalInstance(void)
{
	static xmlWidgetLuaEngine globalInstance;
	return &globalInstance;
}

void xmlWidgetLuaEngine::doLuaAction( const char *script ,xmlWidget* pWidget )
{
	this->insertWidget(pWidget);
	luaL_dofile( m_L,script );
}
xmlWidgetLuaEngine::xmlWidgetLuaEngine()
:m_L(NULL)
,m_RootWidget(NULL)
{
	m_L = luaL_newstate();
	luaL_openlibs(m_L);
	luaL_dostring( m_L," io.output(\"lua.log\") " );



	lua_register(m_L, "widgetOpen", L_widgetOpen);
	lua_register(m_L, "widgetClose", L_widgetClose);
	lua_register(m_L, "fileSelectorGetName", L_fileSelectorGetName);
	lua_register(m_L, "fileSelectorSetName", L_fileSelectorSetName);
	

}
xmlWidgetLuaEngine::~xmlWidgetLuaEngine()
{
	luaL_dostring( m_L," io.close() " );
	lua_close(m_L);
}
xmlWidget* xmlWidgetLuaEngine::searchWidget(const string &key)
{
	xmlWidgetMap::const_iterator it = m_ActionWidget.find(key);
	if( m_ActionWidget.end() != it )
	{
		return it->second;
	}
	if( m_RootWidget )
	{
		xmlWidget* ptemp = m_RootWidget->search(key);
		if(ptemp)
		{
			this->insertWidget(ptemp);
			return ptemp;
		}
	}
	return NULL;
}

void xmlWidgetLuaEngine::insertWidget( xmlWidget* pWidget )
{
	string key = pWidget->getName();
	xmlWidgetMap::const_iterator it = m_ActionWidget.find(key);
	if( m_ActionWidget.end() == it )
	{
		m_ActionWidget[key] = pWidget;
	}

	if( NULL == m_RootWidget )
	{
		xmlWidget* pWidgetTemp = pWidget->getParent();
		while( pWidgetTemp )
		{
			m_RootWidget = pWidgetTemp;
			pWidgetTemp = pWidgetTemp->getParent();
		}
	}
}
